package dal

import (
	"context"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"ruoyi-go/config"
	"ruoyi-go/framework/log"
)

var MinioClient *minio.Client

func InitMinio() {
	minioConfig := config.Data.Minio
	var err error
	MinioClient, err = minio.New(minioConfig.EndPoint, &minio.Options{
		Creds:  credentials.NewStaticV4(minioConfig.AccessKey, minioConfig.SecretKey, ""),
		Secure: false,
	})
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	exists, err := MinioClient.BucketExists(ctx, minioConfig.Bucket)
	if err != nil {
		panic(err)
	}
	if !exists {
		MinioClient.MakeBucket(ctx, minioConfig.Bucket, minio.MakeBucketOptions{})
	}
	log.L.Info("minio init success")
}
